'use strict';

const dataConst = require('../constant/data-const');

const Service = require('egg').Service;

class MeetingService extends Service {
    async getMeetingListSrv(title, limit, offset) {
        const { ctx } = this;

        let queryString =
            `select
                mt.id, mt.title, mt.desc, mt.created_at, mt.updated_at
                , if(isnull(mt.image) || length(trim(mt.image)) < 1, null, concat(:serverUrl, :meetingPath, mt.id, '/', mt.image)) as image_url
                from tbl_meeting mt
                where true
                and mt.state = :state`;

        const paramsData = {
            serverUrl: dataConst.serverUrl,
            meetingPath: dataConst.meetingPath,
            state: dataConst.stateType.active
        };

        if (title) {
            queryString += ` and usr.phone = :title`;
            paramsData.title = `%${title}%`;
        }

        return await ctx.helper.sqlQueryCount(queryString, paramsData, limit, offset);
    }

    async getMeetingDetailSrv(meetingId) {
        const { ctx } = this;

        const queryString =
            `select
                *, if(isnull(mt.image) || length(trim(mt.image)) < 1, null, concat(:serverUrl, :meetingPath, mt.id, '/', mt.image)) as image_url
                from tbl_meeting mt
                where true
                and mt.id = :meetingId`;

        const paramsData = {
            serverUrl: dataConst.serverUrl,
            meetingPath: dataConst.meetingPath,
            meetingId
        };

        const [result] = await ctx.helper.sqlQuery(queryString, paramsData);
        return result;
    }
}

module.exports = MeetingService;
